# vim: filetype=sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.

. $STF_SUITE/include/libtest.kshlib

#
# Create a simple zvol volume
#
# Where disk_device: is the name of the disk to be used
#       volume_size: is the size of the volume, e.g. 2G
#
function default_zvol_setup # disk_device volume_size
{
        typeset disk=$1
        typeset size=$2
	typeset savedumpdev
	typeset -i output

        create_pool $TESTPOOL "$disk"

        log_must $ZFS create -V $size $TESTPOOL/$TESTVOL

	if [[ -n $DUMPADM ]]; then
		if is_dumpswap_supported $TESTPOOL ; then
			set_dumpsize $TESTPOOL/$TESTVOL
		fi
	fi
}

#
# Destroy the default zvol which was setup using
# default_zvol_setup().
#
function default_zvol_cleanup
{
        if datasetexists $TESTPOOL/$TESTVOL ; then
		log_must $ZFS destroy $TESTPOOL/$TESTVOL
	fi

        destroy_pool $TESTPOOL
}

#
# Check if the given pool support "Swap and crash dumps"
#
function is_dumpswap_supported #pool
{
	typeset pool=$1

	if [[ -z $pool ]] ; then
		log_fail "No pool given."
	fi

	typeset -i SPA_VER_DUMPSWAP=10
        typeset -i vp=$(get_pool_prop version $pool)

	if (( vp >= SPA_VER_DUMPSWAP )) ; then
		return 0
	fi

	return 1
}

function get_dumpdevice
{
	typeset ret=$($DUMPADM | $GREP "Dump device:" | $AWK '{print $3}')
	print $ret
}

function set_dumpsize
{
	typeset volume=$1

	if [[ -z $volume ]] ; then
		log_note "No volume specified."
		return 1
	fi

	log_must $ZFS set volsize=64m $volume

	output=$($DUMPADM -d /dev/zvol/$volume 2>&1 | \
			$TAIL -1 | $AWK '{print $3}')

	if [[ -n $output ]]; then
		(( output = output / 1024 / 1024 ))
		(( output = output + output / 5 ))
		log_must $ZFS set volsize=${output}m $volume
	fi
	return 0
}

function safe_dumpadm
{
	typeset device=$1

	if [[ -z $device || $device == "none" ]] ; then
		log_note "No dump device volume specified."
		return 1
	fi
	if [[ $device == "/dev/zvol/"* ]] ; then
		typeset volume=${device#/dev/zvol/}
		set_dumpsize $volume
		log_must $DUMPADM -d $device
	else
		log_must $SWAPADD
		if ! is_swap_inuse $device ; then
			log_must $SWAP -a $device
		fi
		log_must $DUMPADM -d swap
	fi
}

function is_zvol_dumpified
{
	typeset volume=$1

	if [[ -z $volume ]] ; then
		log_note "No volume specified."
		return 1
	fi

	$ZDB -dddd $volume 2 | $GREP "dumpsize" > /dev/null 2>&1
	return $?
}

function is_swap_inuse
{
	typeset device=$1

	if [[ -z $device ]] ; then
		log_note "No device specified."
		return 1
	fi

	$SWAP -l | $GREP -w $device > /dev/null 2>&1
	return $?
}
